<?php
/**
 * Copyright (c) Enalean, 2018. All Rights Reserved.
 *
 * This file is a part of Tuleap.
 *
 * Tuleap is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Tuleap is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Tuleap. If not, see <http://www.gnu.org/licenses/>.
 */

namespace Tuleap\User\AccessKey;

use HTTPRequest;
use Tuleap\Authentication\SplitToken\SplitTokenVerificationStringHasher;
use Tuleap\Cryptography\KeyFactory;
use Tuleap\Layout\BaseLayout;
use Tuleap\Request\DispatchableWithRequest;
use Tuleap\Request\ForbiddenException;

class AccessKeyCreationController implements DispatchableWithRequest
{
    public function process(HTTPRequest $request, BaseLayout $layout, array $variables)
    {
        $current_user = $request->getCurrentUser();
        if ($current_user->isAnonymous()) {
            throw new ForbiddenException(_('Unauthorized action for anonymous'));
        }

        (new \CSRFSynchronizerToken('/account/index.php'))->check();

        $access_key_creator = new AccessKeyCreator(
            new LastAccessKeyIdentifierStore(
                new AccessKeySerializer(),
                (new KeyFactory)->getEncryptionKey(),
                $_SESSION
            ),
            new AccessKeyDAO(),
            new SplitTokenVerificationStringHasher(),
            new AccessKeyCreationNotifier($request->getServerUrl(), \Codendi_HTMLPurifier::instance())
        );

        $description = $request->get('access-key-description') ?: '';

        $access_key_creator->create($current_user, $description);

        $layout->redirect('/account/#account-access-keys');
    }
}
